package com.example.drop

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.drop.adapter.DataAdapter
import com.example.drop.adapter.PointXiangAdapter
import com.example.drop.adapter.TaskAdapter
import com.example.drop.databinding.ActivityPointXiangBinding
import com.example.drop.entity.PointEntityItem
import com.example.drop.vm.DropViewModel
import com.example.drop.vm.PointViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/drop/PointXiangActivity")
class PointXiangActivity : BaseMvvmActivity<ActivityPointXiangBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_point_xiang
    @Autowired
    lateinit var point:PointEntityItem
    val pointViewModel:PointViewModel by viewModels()
    val dropViewModel:DropViewModel by viewModels ()
    val taskAdapter=TaskAdapter()
    var list1= mutableListOf<String>(
        "1 绿地是否需要修剪",
        "2 现场是否有垃圾",
        "3 是否有安全隐患",
        "4 设施是否有损坏")
    val pointXiangAdapter=PointXiangAdapter()
    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        pointViewModel.getPointById(point.id)
        mDataBinding.rv.visibility=View.VISIBLE
        mDataBinding.rv.adapter=pointXiangAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        pointXiangAdapter.addData(list1)
        mDataBinding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->{
                    mDataBinding.rb1.isChecked=true
                    mDataBinding.rb2.isChecked=false
                    mDataBinding.rb3.isChecked=false
                }
                R.id.rb2->{
                    mDataBinding.rb1.isChecked=false
                    mDataBinding.rb2.isChecked=true
                    mDataBinding.rb3.isChecked=false
                }
                R.id.rb3->{
                    mDataBinding.rb1.isChecked=false
                    mDataBinding.rb2.isChecked=false
                    mDataBinding.rb3.isChecked=true
                }
            }
            if (mDataBinding.rb1.isChecked){
                mDataBinding.rv.visibility=View.VISIBLE
                mDataBinding.rv.adapter=pointXiangAdapter
                pointXiangAdapter.data.clear()
                pointXiangAdapter.addData(list1)
                pointXiangAdapter.notifyDataSetChanged()
            }else if (mDataBinding.rb2.isChecked){
                mDataBinding.rv.visibility=View.VISIBLE
                dropViewModel.getTask()
                mDataBinding.rv.adapter=taskAdapter
            }else{
                mDataBinding.rv.visibility=View.GONE
                mDataBinding.tv.visibility=View.VISIBLE
            }
        }
        dropViewModel.mSuccessLiveData.observe(this){
            taskAdapter.data.clear()
            taskAdapter.addData(it.data)
            taskAdapter.notifyDataSetChanged()
        }
        pointViewModel.mSuccessLiveData.observe(this){
           it.data.forEach {
               mDataBinding.pointName.text="项目名称: "+it.pointName
               mDataBinding.projectId.text="点位名称: "+it.projectId
               mDataBinding.pointLocation.text="点位位置: "+it.pointLocation
               if(it.patrolType=="保洁巡检"){
                   mDataBinding.patrolType.setBackgroundResource(R.drawable.bg_blue)
                   mDataBinding.patrolType.setText("保洁巡检")
               }else if(it.patrolType=="安防巡检"){
                   mDataBinding.patrolType.setBackgroundResource(R.drawable.bg_ju)
                   mDataBinding.patrolType.setText("安防巡检")
               }else if(it.patrolType=="日常巡检"){
                   mDataBinding.patrolType.setBackgroundResource(R.drawable.bg_zhizhi)
                   mDataBinding.patrolType.setText("日常巡检")
               }else{
                   mDataBinding.patrolType.setBackgroundResource(R.drawable.bg_red)
                   mDataBinding.patrolType.setText("家居巡检")
               }
           }
        }
        pointViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }
}